package qianting.news.auth.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

/**
 * @program: big-event
 * @des:
 * @author: tzx
 * @create: 2023-11-03 19:19
 */

public class JwtUtil {

    private static final String KEY = "tzx";

    //生成JwtToken
    public static String genToken(Map<String, Object> claims) {
        return JWT.create().withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 12))  //也就是过期时间12个小时
                .sign(Algorithm.HMAC256(KEY));
    }

    //解析token
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

}
